import { PayloadAction, createSlice } from '@reduxjs/toolkit'
import { Channel } from '../../types/channel'
import { AppDispatch } from '../../types/store'
import axios from 'axios'

const channelSlice = createSlice({
  name: 'channel',
  initialState: [] as Channel[],
  reducers: {
    setChannels (_, action: PayloadAction<Channel[]>) {
      console.log('---setChannels---')
      return action.payload
    }
  }
})

const { setChannels } = channelSlice.actions

export const asyncGetChannels = (): any => {
  return async (dispatch: AppDispatch) => {
    const res = await axios.get(
      'http://huangjiangjun.top:3001/api/channel/list'
    )

    dispatch(setChannels(res.data.data))
  }
}

export default channelSlice.reducer
